﻿using Newtonsoft.Json;
using PhysicalExamination5.Interface.SystemSetting;
using PhysicalExamination5.Model.PhysicalExamination;
using PhysicalExamination5.Model.SystemSetting;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PhysicalExamination5.Service.SystemSetting
{
    public class SHeXinXinXi : IHeXinXinXi
    {
        private readonly ISqlSugarClient db;
        public SHeXinXinXi(ISqlSugarClient db)
        {
            this.db = db;
        }
        /// <summary>
        /// 查询核心信息
        /// </summary>
        /// <returns></returns>
        public string GetSelHeXinXinXi()
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】");

                #region 声明变量

                List<object> listHeXinXinXi = new List<object>();

                #endregion

                #region 人员信息相关

                listHeXinXinXi.Add(new
                {
                    Type = 1,
                    Name = "人员信息相关",
                    children = new List<object>
                    {
                        new {Name = "计算年龄",CanShuZHi = new List<object>{ "0","1"},Explain = "默认按年计算：0 按年计算   1 按月计算"},
                        new {Name = "存盘是否自动打印条码指引单",CanShuZHi = new List<object>{ "0","1","2","3"},Explain = "默认0：0 不自动打印   1 自动打印条码指引单  2 自动打印指引单  3  自动打印条码"},
                    }
                });

                #endregion

                #region 费用相关

                listHeXinXinXi.Add(new
                {
                    Type = 1,
                    Name = "费用相关",
                    children = new List<object>
                    {
                        new {Name = "是否开启收费限制",CanShuZHi = new List<object>{ "N","Y"},Explain = "Y 为开启收费限制 N为不开启收费限制  默认为N"}
                    }
                });

                #endregion

                #region 总检相关

                listHeXinXinXi.Add(new
                {
                    Type = 1,
                    Name = "总检相关",
                    children = new List<object>
                    {
                        new {Name = "总检结论文字是否不体现未检项目",CanShuZHi = new List<object>{ "N","Y"},Explain = "Y 为不体现未检项目 N为体现未检项目  默认为N"},
                        new {Name = "总检结论文字是否不体现其余所检项目正常",CanShuZHi = new List<object>{ "N","Y"},Explain = "Y 为不体现其余所检项目正常 N为体现其余所检项目正常  默认为N"}
                    }
                });

            #endregion

            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = listHeXinXinXi
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 查询核心信息参数值
        /// </summary>
        /// <returns></returns>
        public string GetSelCanShuZhi(string strBiaoShi)
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】");

                string strCanShuZhi = "";

                if (string.IsNullOrEmpty(strBiaoShi))
                {
                    nret = 0;
                    strFanHuiZhi = "标识为空！";
                    goto PROC_EXIT;
                }

                strCanShuZhi = new SqlSugarRepository<HeXinXinXi>(db).getByWhere(t => t.BiaoShi == strBiaoShi).Select(t => t.CanShuZhi).FirstOrDefault();


            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = strCanShuZhi
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
        /// <summary>
        /// 保存核心信息
        /// </summary>
        /// <param name="saveHeXinXinXi"></param>
        /// <returns></returns>
        public string PostSaveHeXinXinXi(SaveHeXinXinXiClass saveHeXinXinXi)
        {
            int nret = 0; string strFanHuiZhi = "";
            try
            {
                Logger.Singleton.Debug($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】");

                if (new SqlSugarRepository<HeXinXinXi>(db).DeleteWhere(t => t.BiaoShi == saveHeXinXinXi.Name) == false)
                {
                    nret = 1000;
                    strFanHuiZhi = "删除失败！";
                    goto PROC_EXIT;
                }
                HeXinXinXi heXinXinXi = new HeXinXinXi();
                heXinXinXi.BiaoShi = saveHeXinXinXi.Name;
                heXinXinXi.CanShuZhi = saveHeXinXinXi.CanShuZhi;
                heXinXinXi.ShuoMing = saveHeXinXinXi.Explain;
                if (new SqlSugarRepository<HeXinXinXi>(db).Add(heXinXinXi) == false)
                {
                    nret = 1000;
                    strFanHuiZhi = "添加失败！";
                    goto PROC_EXIT;
                }


            PROC_EXIT:
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = nret,
                    Message = strFanHuiZhi,
                    Data = ""
                });
            }
            catch (Exception ex)
            {
                Logger.Singleton.Error($@"【{System.Reflection.MethodBase.GetCurrentMethod().Name}】:报错异常:" + ex.Message);
                return JsonConvert.SerializeObject(new ApiResult<object>()
                {
                    Success = 1000,
                    Message = ex.Message,
                    Data = ""
                });
            }
        }
    }
}
